import pymysql

from util.lib.TYPE import TYPE


class Connection:
    def __init__(self, host, user, password, db, port=3306, charset='utf8mb4'):
        self.conn = pymysql.connect(
            host=host, user=user, passwd=password, port=port, charset=charset
        )
        self.dbName = db
        self.conn.select_db(self.dbName)

    def close(self):
        self.conn.close()

    def cursor(self):
        return self.conn.cursor()

    def rollback(self):
        self.conn.rollback()

    def commit(self):
        self.conn.commit()

    def getObjArrBySql(self, sql):
        table = self.conn.cursor()
        table.execute(sql)
        rows = table.fetchall()
        if TYPE.is_tuple(rows):
            return rows
        else:
            return False
